package icu.zqyhdm.blog.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import icu.zqyhdm.blog.service.ArticleService;
import icu.zqyhdm.blog.vo.*;
import icu.zqyhdm.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


/**
 * @Description
 * @Author lifu
 * @Date 2021/6/22 21:20
 */
@Api(tags = "博客的文章接口")
@RestController
@RequestMapping(value = "/api/blog/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @ApiOperation(value = "根据名称对文章进行模糊查询,亦用于首页的文章展示")
    @GetMapping(value = "/getListByName/{current}/{limit}")
    public Result selectArticlePage(@PathVariable Integer current,
                               @PathVariable Integer limit,
                               ArticleQueryVo articleQueryVo){
        Page<ArticleListVo> articlePage = articleService.selectPageByName(current,limit,articleQueryVo);
        return Result.ok(articlePage);
    }

    @ApiOperation(value = "根据id对文章进行精确查询")
    @GetMapping(value = "/getListById/{current}/{limit}")
    public Result selectArticlePageById(@PathVariable Integer current,
                                 @PathVariable Integer limit,
                                 ArticleQueryIdVo articleQueryIdVo){
        Page<ArticleListVo> articlePage = articleService.selectPageById(current,limit,articleQueryIdVo);
        return Result.ok(articlePage);
    }

    @ApiOperation(value = "查询文章详情")
    @GetMapping(value = "/showDetail/{id}")
    public Result showArticleDetail(HttpServletRequest request, @PathVariable Integer id){
        // 先从缓存查mysql的基本静态数据
        ArticleDetailVo articleDetailVo = articleService.getArticleById(id);
        // 从再redis查浏览数、点赞数动态数据
        articleService.getDynamicData(articleDetailVo);
        // 异步操作，更新浏览量
        articleService.updateArticleViews(request,id);
        return Result.ok(articleDetailVo);
    }

    @ApiOperation(value = "保存或更新文章")
    @PostMapping(value = "/save")
    public Result saveOrUpdateArticle(@RequestHeader("X-Token") String token, @Valid @RequestBody ArticleUploadVo articleUploadVo){
        if(articleUploadVo.getId() == null){
            return Result.ok(articleService.saveArticle(token,articleUploadVo));
        }else {
            return Result.ok(articleService.updateArticle(token,articleUploadVo));
        }
    }

    @ApiOperation(value = "删除文章")
    @DeleteMapping(value = "/delete/{id}")
    public Result delete(@RequestHeader("X-Token") String token, @PathVariable("id") Integer id){
        articleService.deleteArticle(token,id);
        return Result.ok();
    }

    // 点赞文章
    @ApiOperation(value = "点赞文章")
    @GetMapping(value = "/like/{id}")
    public Result likeArticle(@RequestHeader("X-Token") String token, @PathVariable Integer id){
        // 异步操作，更新文章点赞量和用户的点赞列表
        articleService.likeArticle(token,id);
        return Result.ok();
    }

    @ApiOperation(value = "热点文章，根据浏览量查")
    @GetMapping(value = "/hot/{current}/{limit}")
    public Result getHotArticleList(@PathVariable Integer current,
                                    @PathVariable Integer limit) {
        Page<ArticleHotVo> articlePage = articleService.selectHotArticle(current, limit);
        return Result.ok(articlePage);
    }

    @ApiOperation(value = "日期数量归档")
    @GetMapping(value = "/createTime/{current}/{limit}")
    public Result getCreateTimeList(@PathVariable Integer current,
                                    @PathVariable Integer limit){
        Page<CreateTimeVo> page = articleService.selectCreateTimeList(current,limit);
        return Result.ok(page);
    }

    @ApiOperation(value = "归档")
    @GetMapping(value = "/archive/{current}/{limit}")
    public Result getArchiveList(@PathVariable Integer current,
                                    @PathVariable Integer limit){
        Page<ArticleInfoVo> page = articleService.getArchiveList(current,limit);
        return Result.ok(page);
    }

    //上传文件到阿里云oss
    @ApiOperation(value = "上传文件到阿里云oss")
    @PostMapping(value = "/fileUpload/{articleId}")
    public Result fileUpload(@RequestHeader("X-Token") String token, @RequestParam(value = "file") MultipartFile multipartFile,@PathVariable Integer articleId){
        String url = articleService.upload(token,multipartFile,articleId);
        return Result.ok(url);
    }

}
